﻿using Admin.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace Admin.Controllers
{
    public class UsersController : BaseController
    {
        //
        // GET: /Users/
        private readonly IusersService _users;
        public UsersController(IusersService users)
        {
            _users = users;
        }
        //
        // GET: /Users/
        public ActionResult Index(int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount;
            var model = _users.GetMany(s => true, Order.desc, "CreateTime");
            model = _users.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return View(model);
        }

        public ActionResult Search(string UserName, string Email, string Mobile, int? usertype = 0, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount;
            var model = _users.GetMany(s => true, Order.desc, "CreateTime");
            model = QueryHelper.WhereIf<users>(model, s => s.UserName.Contains(UserName), !string.IsNullOrWhiteSpace(UserName));
            model = QueryHelper.WhereIf<users>(model, s => s.Email.Equals(Email), !string.IsNullOrWhiteSpace(Email));
            model = QueryHelper.WhereIf<users>(model, s => s.Mobile.Contains(Mobile), !string.IsNullOrWhiteSpace(Mobile));
            model = _users.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("List", model);
        }

       

        public ActionResult Edit(int uid, int? usertype = 0)
        {
            var model = _users.Get(s => s.Id == uid);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(users users)
        {
            if (ModelState.IsValid)
            {
                var userpwd = Request["userpwd"];
                if (!string.IsNullOrEmpty(userpwd))
                {
                    String newError = ValidatePwd.Valid(userpwd);
                    if (!string.IsNullOrEmpty(newError))
                        return new JsonResult() { Data = new { d = false, msg = newError } };
                    else
                    {
                        var passwordSalt = SecurityHelper.CreateSalt();
                        var password = Cryptographer.EncodePassword(userpwd, 1, passwordSalt);
                        users.Pwd = password;
                        users.EncryptContent = passwordSalt;
                    }
                }
                _users.Update(users);
            }
            return new JsonResult() { Data = new { d = true } };
        }


        public ActionResult Add(string usertype)
        {
            ViewBag.usertype = usertype;
            return View();
        }
        [HttpPost]
        public ActionResult Add(users users)
        {
            if (ModelState.IsValid)
            {
                users.type = 0;
                users.CreateTime = System.DateTime.Now;
                String newError = ValidatePwd.Valid(users.Pwd);
                if (!string.IsNullOrEmpty(newError))
                    return new JsonResult() { Data = new { d = false, msg = newError } };
                else
                {
                    var passwordSalt = SecurityHelper.CreateSalt();
                    var password = Cryptographer.EncodePassword(users.Pwd, 1, passwordSalt);
                    users.Pwd = password;
                    users.EncryptContent = passwordSalt;
                }
                _users.Add(users);
            }
            return new JsonResult() { Data = new { d = true } };
        }
        public ActionResult Detail(int id)
        {
            return View();
        }

        public JsonResult Delete(int id)
        {
            var model = _users.Get(s => s.Id == id);
            _users.Delete(model);
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
	}
}